/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ohos.rajawali3d.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.RajaItemProvider;
import org.ohos.rajawali3d.examples.data.DataSet;
import org.ohos.rajawali3d.examples.data.RajawaBean;

import java.util.ArrayList;
import java.util.List;

/**
 * MainAbilitySlice
 */
public class MainAbilitySlice extends AbilitySlice {

    private ListContainer listContainer;

    private RajaItemProvider rajaItemProvider;

    private ArrayList<RajawaBean> rajaData;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_main_layout);

        initComponents();
        initExampleData();
        initProvider();
    }

    private void initComponents() {
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
    }

    private void initProvider() {
        rajaItemProvider = new RajaItemProvider(this, rajaData);
        listContainer.setItemProvider(rajaItemProvider);
        listContainer.setReboundEffect(false);
    }

    private void initExampleData() {
        rajaData = new ArrayList<>();
        DataSet ds = DataSet.getInstance();
        List<RajawaBean> beans = ds.getRajawaBeans();
        for (int i = 0; i < beans.size(); i++) {
            addExample(beans.get(i));
        }
    }

    private void addExample(RajawaBean bean) {
        rajaData.add(bean);
    }
}
